// Ideology/partisanship scales

log using IdeologyANES20.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2020_stata_20220210.dta" // American National Election Study 2020 Time Series Study, Feb 10, 2022 Version. Date accessed: March 09, 2025. Accessed from https://electionstudies.org/data-center/2020-time-series-study/

// Social justice scale 
*Delete missing values
rename V201411x tgpolicy 
replace tgpolicy = . if tgpolicy == -2
rename V201626 offence 
replace offence = . if inlist(offence, -5, -9)
rename V202183 metoo 
replace metoo = . if inlist(metoo, -9, -7, -6, -5, -4, 998, 999)
rename V202174 blm 
replace blm = . if inlist(blm, -9, -7, -6, -5, -4, 998, 999)

*Reverse code offence so social justice values are high
egen maxval = max(offence)
gen roffence = maxval + 1 - offence
drop maxval

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in tgpolicy roffence metoo blm {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in stgpolicy sroffence smetoo sblm  {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stgpolicy sroffence smetoo sblm  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
*Delete missing values

rename V201415 gayadopt
replace gayadopt = . if inlist(gayadopt, -8, -9)
rename V201336 abortion
replace abortion = . if inlist(abortion, -8, -9, 5) 
rename V201262 environment
replace environment = . if inlist(environment, -9, -8, 99)
rename V202232 immigration
replace immigration = . if inlist(immigration, -9, -8, -7, -6, -5) 
rename V201345x deathpen
replace deathpen = . if inlist(deathpen, -2) 
rename V201426x wall
replace wall = . if inlist(wall, -2) 
rename V201308x bordersec
replace bordersec = . if inlist(bordersec, -2) 
rename V201311x crime
replace crime = . if inlist(crime, -2) 

*Reverse variables so liberalism coded high
foreach var in gayadopt immigration environment {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rgayadopt abortion renvironment rimmigration deathpen wall bordersec crime {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = .
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// LeftRight Scale
rename V201302x socsec  
rename V201314x welfare  
rename V201320x aidtopoor 
rename V202255x lessgov  
rename V202259x incomeinequality  
rename V202256 govreg 
rename V202325 taxmillionaires 
rename V202326 affordablecare 
rename V202376x guarincome 
rename V202380x healthcare  
rename V202426 incomediffs 

foreach var in socsec welfare aidtopoor lessgov incomeinequality govreg taxmillionaires affordablecare guarincome healthcare incomediffs {
    replace `var' = . if `var' < 0
}

*Reverse/recode variables so left is coded high
recode taxmillionaires (1 = 3) (2 = 1) (3 = 2), generate(rtaxmillionaires)
recode affordablecare  (1 = 3) (2 = 1) (3 = 2), generate(raffordablecare)

foreach var in socsec welfare aidtopoor incomeinequality govreg guarincome healthcare incomediffs {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rsocsec rwelfare raidtopoor lessgov rincomeinequality rgovreg rtaxmillionaires rguarincome rhealthcare rincomediffs raffordablecare {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srsocsec srwelfare sraidtopoor slessgov srincomeinequality srgovreg srtaxmillionaires srguarincome srhealthcare srincomediffs sraffordablecare {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreLR = 0
gen count_nonzeroLR = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srsocsec srwelfare sraidtopoor slessgov srincomeinequality srgovreg srtaxmillionaires srguarincome srhealthcare srincomediffs sraffordablecare {
    replace total_scoreLR = total_scoreLR + `var'
    replace count_nonzeroLR = count_nonzeroLR + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LeftValues = .
replace LeftValues = total_scoreLR / count_nonzeroLR if count_nonzeroLR > 0

// Lib-con ideological self-placement scale
*Clean the scale
rename V201200 libconsp
replace libconsp = . if inlist(libconsp, -9, -8, 99) 

*Reverse so liberalism coded high
egen maxval = max(libconsp)
gen LibConSP = maxval + 1 - libconsp
drop maxval

//Democratic Party feeling
rename V201156 Demfeeling 
replace Demfeeling=. if inlist(Demfeeling, -9, 998) 

// Correlations
pwcorr SocJusValues LibValues LeftValues Demfeeling LibConSP [aweight=V200010b], sig

// Ideological constraint

*Create graduate dummy variable
rename V201510 education
replace education = . if inlist(education, -9, -8, 95)

gen Graduate=.
replace Graduate=0 if education<6
replace Graduate=1 if inrange(education, 6, 8)

pwcorr tgpolicy Roffence metoo blm [aweight=V200010b], sig 
pwcorr tgpolicy Roffence metoo blm if V201005==1 [aweight=V200010b], sig 
pwcorr tgpolicy Roffence metoo blm if Graduate==1 [aweight=V200010b], sig 
pwcorr tgpolicy Roffence metoo blm if LibConSP>4 [aweight=V200010b], sig 
pwcorr tgpolicy Roffence metoo blm if V201231x==1 [aweight=V200010b], sig 

log close
